package work.xiaohong.puke;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import work.xiaohong.puke.netty.WsServerInitializer;

@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
public class PuKeApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(PuKeApplication.class,args);

        //启动netty
        NioEventLoopGroup manGroup = new NioEventLoopGroup();
        NioEventLoopGroup subGroup = new NioEventLoopGroup();
        try {
            // 创建服务器启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(manGroup,subGroup) //设置主从线程组
                    .channel(NioServerSocketChannel.class) // 设置Nio双向通道
                    .childHandler(new WsServerInitializer()); // 添加子处理器，用于处理从线程池的任务
            ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
            channelFuture.channel().closeFuture().sync();
        }finally {
            manGroup.shutdownGracefully();
            subGroup.shutdownGracefully();
        }
    }
}
